package cn.suimg.shortlink.config;

import cn.suimg.shortlink.filter.IndexFilter;
import cn.suimg.shortlink.interceptor.FirewallInterceptor;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Web Config
 *
 * @author suimg
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
     * http自动跳转https
     * @return
     */
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(new Connector("org.apache.coyote.http11.Http11NioProtocol"){{
            setScheme("http");
            setPort(80);
            setSecure(false);
            setRedirectPort(443);
        }});
        return tomcat;
    }

    /**
     * 过滤静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/favicon.ico");
    }


    /**
     * 配置数据库连接池
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource(){
        return new DruidDataSource();
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new FirewallInterceptor())
                .addPathPatterns("/**")
        ;
        super.addInterceptors(registry);
    }

    /**
     * 配置过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        return new FilterRegistrationBean(){{
            setFilter(new IndexFilter());
            addUrlPatterns("/*");
            setName("mainFilter");
        }};
    }

    /**
     * 配置Redis连接池配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.config")
    public JedisPoolConfig getJedisPoolConfig(){
        return new JedisPoolConfig();
    }

    /**
     * 配置Redis连接池
     * @param config
     * @param host
     * @param port
     * @return
     */
    @Bean
    public JedisPool getJedisPool(JedisPoolConfig config, @Value("${spring.redis.host}") String host, @Value("${spring.redis.port}") Integer port){
        return new JedisPool(config,host,port);
    }

}